#include "State_Menu.h"
//#include "State_GamePlay.h"

extern IDirectInputDevice8		*mJoystick[2]; // This will be changed to an area later...
extern DIJOYSTATE2				js;
//extern 	int prevJSButtons[12];
//extern 	int prevJSButtons2[12];
//extern 	int prevJS;
extern 	ID3DXSprite* mSprite;
extern CDirectSound sound;

State_Menu* State_Menu::init = 0;

State_Menu::State_Menu()
{
	HR(D3DXCreateTextureFromFile(gd3dDevice, "MazeMenuTest01.png", &mBkgdTex));
	mBkgdCenter = D3DXVECTOR3(640.0f, 400.0f, 0.0f);
	
	//set up camera info
	mCameraRadius    = 10.0f;
	mCameraRotationY = 1.2 * D3DX_PI;
	mCameraHeight    = 5.0f;
	sound.PlayMusic(BigBallsMusic);
	
}

void State_Menu::Enter()
{
//	cout << "State_Menu has been entered\n";
	choice = '0';


}

void State_Menu::Execute(D3DApp* g)
{
			
	static float cursorDelay = 0.0f;
	static float timeAccum = 0.0f;
	static int prevJS = 0;
	mJoystick[0]->GetDeviceState(sizeof(DIJOYSTATE2), &js);
		
	if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 0) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "1st", "Error", 0);
		MessageBox(gd3dApp->getMainWindowHandle(), "1st", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 9000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "2nd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 18000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "3rd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 27000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "4th", "Error", 0);
	}
	else if((js.rgbButtons[0] & 0x80) && (prevJSButtons[0] ^ js.rgbButtons[0]))
	{
		gd3dApp->setState(TwoPlayer::Instance());
	}

	else if(js.rgbButtons[1] & 0x80 && (prevJSButtons[1] ^ js.rgbButtons[1]))
	{
		gd3dApp->setState(ThreePlayer::Instance());
	}

	else if(js.rgbButtons[2] & 0x80 && (prevJSButtons[2] ^ js.rgbButtons[2]))
	{
		gd3dApp->setState(FourPlayer::Instance());
	}

	else if(js.rgbButtons[3] & 0x80 && (prevJSButtons[3] ^ js.rgbButtons[3]))
	{
		//PostQuitMessage(0);
	}
	else if(js.rgbButtons[4] & 0x80 && (prevJSButtons[4] ^ js.rgbButtons[4]))
	{
		PostQuitMessage(0);
	}

	if(!(js.rgdwPOV[0] == 4500 || js.rgdwPOV[0] == 13500 || js.rgdwPOV[0] == 22500 || js.rgdwPOV[0] == 31500))
			prevJS = js.rgdwPOV[0];

	for(int i = 0; i < 12; i++)
	{
		prevJSButtons[i] = js.rgbButtons[i];
	}



	Draw();
}

void State_Menu::Exit()
{
//	cout << "Exiting State_Menu\n";
}

void State_Menu::Draw()
{

	setupCamera();

    // Now we can clear just view-port's portion of the buffer to red...
   HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

    HR(gd3dDevice->BeginScene());
    /*buildViewMtx();

	D3DXMATRIX W;
	D3DXMatrixIdentity(&W);
	HR(gd3dDevice->SetTransform(D3DTS_WORLD, &W));
	HR(gd3dDevice->SetTransform(D3DTS_VIEW, &mView));
	HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj));*/

	



    //draw stuff here
	HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

	D3DXMATRIX texScaling;
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, -260.0f, -140.0f, 0.0f);
	D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	//draw the menu
	HR(mSprite->Draw(mBkgdTex, 0, &mBkgdCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());

	//restore the texture coordinate scaling transform. mirror texture 
	//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	//anything that needs to be drawn should be above this line and below the mSprite->Begin line
	HR(mSprite->End());

	HR(gd3dDevice->EndScene());
	


	//present the back buffer
	HR(gd3dDevice->Present(0, 0, 0, 0));
}

State_Menu* State_Menu::Instance()
{
	if(init == 0)
	{
		init = new State_Menu();
	}
	return init;
}

//the following functions help set up the camera and the world view
void State_Menu::buildViewMtx()
{
	float x = mCameraRadius * cosf(mCameraRotationY);
	float z = mCameraRadius * sinf(mCameraRotationY);
	D3DXVECTOR3 pos(0.0f, 0.0f, -1000.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
}

void State_Menu::buildProjMtx()
{
	float w = WIDTH;
	float h = HEIGHT;
	D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI * 0.25f, w/h, 1.0f, 5000.0f);
}

void State_Menu::setupCamera()
{
		//sets up the camera 1000 units back looking at the origin
	D3DXMATRIX V;
	D3DXVECTOR3 pos(0, 0, -1000.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXMatrixLookAtLH(&V, &pos, &target, &up);
	HR(gd3dDevice->SetTransform(D3DTS_VIEW, &V));

	//The following code defines the volume of space the camera sees
	D3DXMATRIX P;
	RECT R;
	
	float width = WIDTH;
	float height = HEIGHT;
	D3DXMatrixPerspectiveFovLH(&P, D3DX_PI*0.25f, width/height, 1.0f, 5000.0f);
	HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &P));

	
	

	//this line of code disables Direct3D Lighting
	HR(gd3dDevice->SetRenderState(D3DRS_LIGHTING, false));


	//indicates that we are using 2D texture coordinates
	HR(gd3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2));
}